import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '@/views/LoginView.vue'
// import isLogin from '@/utils/login';
import pinia from "@/stores/pinia.js"
import { useLoginStore } from '@/stores/login';

  // const store = useLoginStore(pinia);

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      alias:'/login/',
      name:'login',
      component:LoginView,
      // 有三种方式
      props:true,
      meta:{name:'zp'}
    },
    {
      path:'/home',
      // redirect:'/home/products',
      name:'home',
      component:()=> import("@/views/HomeView.vue"),
      // requiresLogin:需要登陆授权
      meta:{title:'商品管理'},
      children:[
      {
        path:'/home/products',
        name:'products',
        component: ()=> import('@/views/products/ProductsInfo.vue'),
        meta:{requiresLogin:true, title:'商品列表'},
        children:[
          
        ]
      },
      
      {
        path:'/home/sort',
        name:'sort',
        component: ()=> import('@/views/products/ProductsSort.vue'),
        meta:{requiresLogin:true , title:'商品分类'},

        
      },
      {
        path:'/home/products/:id',
        name:'detail',
        component: ()=> import('@/views/products/ProductDetail.vue')
      },
      
    ]
    }
  ]
});


//全局守卫
router.beforeEach((to,from,next)=>{
  const store = useLoginStore(pinia);
  if(to.meta.requiresLogin && !store.isLogin() ){
    next({name:"login"})
  }
 else{
  next();
 }
});


export default router
